--require("ttbuild_portable")
require("ttbuild_core")
require("uuid")
-- ttbuild.home = "C:/local-TaoTie-dev/src/Root"
-- ttbuild.showcmd = true
-- compile simple c code
local flags = {}
local source = {}
local target = nil
for i,v in pairs(arg) do
	local j,k = v:find("%-.*")
	if j == 1 then
		table.insert(flags,v)
	else
		local l,m = v:find(".*%.c$")
		if l and m then
			table.insert(source,v)
		end
	end
	local j,k = v:find("%-o.*")
	if j == 1 then
		target = arg[i+1]
	end
end
if nil == target then
	target = "a.exe"
end

local basename = uuid.generate()
local temp_bc = {}
local temp_bcobj = basename..".bc"
local temp_asm = basename..".asm"
local temp_obj = basename..".obj"

for i,v in pairs(source) do
	local obj = v..".bc"
	table.insert(temp_bc,obj)
	local ret = ttbuild:bc_c(v,obj,"","tt-cc.log")
	if -1 == ret then
		os.exit(-1)
	end 
end
local ret = ttbuild:bc_link(table.concat(temp_bc," "),temp_bcobj," ", "tt-cc.log")
if -1 == ret then
	os.exit(-1)
end 
local ret = ttbuild:llc(temp_bcobj,temp_asm," ", "tt-cc.log")
if -1 == ret then
	os.exit(-1)
end 
local ret = ttbuild:as(temp_asm,temp_obj," ", "tt-cc.log")
if -1 == ret then
	os.exit(-1)
end 
local ret = ttbuild:ld_c(temp_obj,target," ","tt-cc.log")
if -1 == ret then
	os.exit(-1)
end 

for i,v in pairs(temp_bc) do
	os.remove(v)
end
os.remove(temp_bcobj)
os.remove(temp_asm)
os.remove(temp_obj)
os.remove("tt-cc.log")
